﻿using AutoMapper;
using MediatR;
using SlaughterItem.Common.Domains.Setup;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.Setups
{
    /// <summary>
    /// 扣减项设置删除
    /// </summary>
    public class DeductionsDeleteHandler : IRequestHandler<DeductionsDeleteCommand, Result>
    {
        /// <summary>
        /// 扣减项仓储
        /// </summary>
        private readonly IBaseRepository<Deductions> deductionsRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="waitingsRepository">扣减项仓储</param>
        public DeductionsDeleteHandler(IBaseRepository<Deductions> deductionsRepository)
        {
            this.deductionsRepository = deductionsRepository;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(DeductionsDeleteCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Delete };

            var list = deductionsRepository.GetAll().Where(x => x.DeductionId == request.DeductionId).FirstOrDefault();
            if (list != null)
            {
                list.IsDel = 1;

                deductionsRepository.Update(list);
            }

            result.Code = (int)CommonErrorCode.Success;
            return result;
        }
    }
}
